package mapkit
{
	import mx.core.UIComponent;

	public class MapLayer extends UIComponent
	{
		private var _map:Map=null;

		final protected function get map():Map
		{
			return _map;
		}

		private var _ready:Boolean=false;

		final public function get ready():Boolean
		{
			return _ready;
		}

		final public function start(map_:Map):void
		{
			if (_ready)
			{
				return;
			}
			_map=map_;
			for each (var htype:Class in handlerTypes)
			{
				var handler:MapHandler=map.getHandler(htype);
				if (!handler)
				{
					handler=new htype();
					map.enableHandler(handler);
				}
			}
			load();
			dispatchEvent(new MapLayerEvent(MapLayerEvent.LOAD, this, map));
			_ready=true;
		}

		final public function stop():void
		{
			if (!_ready)
			{
				return;
			}
			unload();
			dispatchEvent(new MapLayerEvent(MapLayerEvent.UNLOAD, this, map));
			_map=null;
			_ready=false;
		}

		virtual public function get handlerTypes():Array
		{
			return [];
		}

		virtual protected function load():void
		{
			map.addEventListener(MapEvent.CHANGE, updateMap);
			updateMap(null);
		}

		virtual protected function unload():void
		{
			map.removeEventListener(MapEvent.CHANGE, updateMap);
		}

		virtual protected function updateMap(event:MapEvent=null):void
		{
			width=map.width;
			height=map.height;
		}

	}
}